package com.yanqu.road.utils;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.yanqu.road.connect.AgentServerConnectionGroup;
import com.yanqu.road.task.ReconnectTask;

public class ReconnectUtils {
	
	private static final ReconnectUtils instance = new ReconnectUtils();

	private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;

	private ReconnectUtils() {
		scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
	}

	public static ReconnectUtils getInstance() {
		return instance;
	}

	public void reconnect(AgentServerConnectionGroup connectionGroup, int time) {
		ReconnectTask reconnectTask = new ReconnectTask(connectionGroup);
		ScheduledFuture<?> scheduledFuture = scheduledThreadPoolExecutor.scheduleAtFixedRate(reconnectTask, time, time, TimeUnit.SECONDS);
		reconnectTask.setFuture(scheduledFuture);
	}
}
